package sprites
{
	import UIComponent.LineSkeleton;
	import UIComponent.PointSkeleton;
	import UIComponent.PointSprite;
	
	import flash.events.MouseEvent;
	
	import mx.charts.chartClasses.DataDescription;
	import mx.collections.ArrayList;
	import mx.controls.Button;
	import mx.events.PropertyChangeEvent;
	
	import skeletons.DKRelationSkeleton;
	import skeletons.DataSourceSkeleton;
	import skeletons.KnowledgeSkeleton;

	public class DataSourceSprite extends PointSprite
	{	
		//component
		private var highlight:Button;
		private var deriveDisconnect:Button;
		
		//status
		private var showDeriveDisconnect:Boolean=false;
		private var deriveDisconnectRelation:DKRelationSkeleton;
		
		public function DataSourceSprite(_dataSource:DataSourceSkeleton, _canvas:MyCanvas)
		{
			super(_dataSource,_canvas);
			this.setColor(0x028685);
			
			//component
			highlight=new Button();
			this.addChild(highlight);
			highlight.label="k";
			highlight.x = DEFAULT_MINI_WIDTH - MARGIN_WIDTH*3 - CONVERT_WIDTH - REMOVE_WIDTH - BUTTON_WIDTH;
			highlight.y = MARGIN_HEIGHT;
			highlight.width= BUTTON_WIDTH;
			highlight.height = DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			highlight.addEventListener(MouseEvent.CLICK,onTraceDerive);
			
			deriveDisconnect=new Button();
			this.addChild(deriveDisconnect);
			deriveDisconnect.label="";
			deriveDisconnect.x= getWidth();
			deriveDisconnect.width= BUTTON_WIDTH/2;
			deriveDisconnect.height= DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
			deriveDisconnect.y= MARGIN_HEIGHT;
			deriveDisconnect.addEventListener(MouseEvent.CLICK,onRemoveDKRelation);
			deriveDisconnect.visible=false;

			//event
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this.addEventListener(MouseEvent.DOUBLE_CLICK,onTraceDerive);
			
			//test
			increaseLife();
			
		}
		
		override protected function onSelected(evt:MouseEvent):void
		{
			if(canvas.getSelectedData()==this)
			{
				canvas.setSelectedData(null);
				this.selected=false;
				this.invalidateDisplayList();
			}
			else
			{
				if(canvas.getSelectedData()==null)
				{
					//do nothing
				}
				else
				{
					canvas.getSelectedData().selected=false;
					canvas.getSelectedData().invalidateDisplayList();
				}
				canvas.setSelectedData(this);
				this.selected=true;
				this.invalidateDisplayList();
			}
		}
		
		override public function remove():void
		{
			canvas.removeChild(this);
		}
		
		override protected function onRemove(evt:MouseEvent):void
		{
			super.onRemove(evt);
			canvas.curProject.removeData((DataSourceSkeleton)(this.getSkeleton()));
		}
		
		
		protected function onMouseDown(evt:MouseEvent):void
		{
			canvas.setMouseDownObject(this);
		}
		
		protected function onMouseUp(evt:MouseEvent):void
		{
			var mouseDownObject=canvas.getMouseDownObject();
			if(mouseDownObject==null)
			{
				return;
			}
			
			if(mouseDownObject is KnowledgeSprite)
			{				
				var _dataSource:DataSourceSkeleton = (DataSourceSkeleton)(this.getSkeleton());
				var _knowledge:KnowledgeSkeleton= (KnowledgeSkeleton)((KnowledgeSprite)(mouseDownObject).getSkeleton());
				
				canvas.curProject.sendAddDKRelation(_dataSource.getDID(), _knowledge.getKID(), canvas.getSelectedLinkType());
			}
		}
		
		protected function onTraceDerive(evt:MouseEvent):void
		{
			if(canvas.getTrackingObject()!=null)
			{
				(PointSprite)(canvas.getTrackingObject()).invalidateDisplayList();
			}
			
			onSetTrace();
			
//			if(canvas.getTrackingObject()==null || canvas.getTrackingObject()!= this)
//			{
//				canvas.setTrackingObject(this);
//				//traceDerive(true);
//			}
//			else
//			{
//				canvas.setTrackingObject(null);
				
//				var tracingObject:Object=canvas.getTrackingObject();
//				if( tracingObject is DataSourceSprite)
//				{
//					var tracingData:DataSourceSprite =(DataSourceSprite)(tracingObject);
//					if( tracingData == this)
//					{
//						traceDerive(false);
//						canvas.setTrackingObject(null);
//					}
//					else
//					{
//						tracingData.traceDerive(false);
//						
//						canvas.setTrackingObject(this);
//						traceDerive(true);
//					}
//				}
//				else if(tracingObject is KnowledgeSprite)
//				{
//					var tracingKnowledge:KnowledgeSprite = (KnowledgeSprite)(tracingObject);
//					tracingKnowledge.traceDerive(false);
//					tracingKnowledge.traceFrom(false);
//					
//					canvas.setTrackingObject(this);
//					traceDerive(true);
//				}
//				else if(tracingObject is ConclustionSprite)
//				{
//					var tracingCon:ConclustionSprite = (ConclustionSprite)(tracingObject);
//					tracingCon.traceFrom(false);
//					
//					canvas.setTrackingObject(this);
//					traceDerive(true);
//				}
//			}
			canvas.invalidateDisplayList();
		}
		
		public function traceDerive(_isSelected:Boolean):void
		{
			setHighlight(_isSelected);
			this.invalidateDisplayList();
			var DKList:ArrayList = ((DataSourceSkeleton)(this.getSkeleton())).getDKList();
			if(DKList==null)
			{
				return;
			}
			var i:Number=0;
			for(i=0; i< DKList.length; i++)
			{
				var curDK:DKRelationSkeleton=(DKRelationSkeleton)(DKList.getItemAt(i));
				curDK.getSprite().setSelected(_isSelected);
				//curDK.getToPointSkeleton().getSprite().setHighlight(_isSelected);
				//to be continued
				
				(KnowledgeSprite)(curDK.getToPointSkeleton().getSprite()).traceDerive(_isSelected);
				
				if(canvas.getTrackingObject() is DataSourceSprite)
				{
					(KnowledgeSprite)(curDK.getToPointSkeleton().getSprite()).showFromConnect(curDK,_isSelected);
				}
			}
		}
		
		public function showDeriveConnect(_DKRelation:DKRelationSkeleton, _isShow:Boolean):void
		{
			if(_isShow == true)
			{
				deriveDisconnect=new Button();
				this.addChild(deriveDisconnect);
				deriveDisconnect.label="";
				deriveDisconnect.x= getWidth();
				deriveDisconnect.width= BUTTON_WIDTH/2;
				deriveDisconnect.height= DEFAULT_MINI_HEIGHT - MARGIN_HEIGHT*2;
				if(expand==true)
				{
					deriveDisconnect.y= this.getHeight()/2 - deriveDisconnect.height/2;
				}
				else
				{
					deriveDisconnect.y= MARGIN_HEIGHT;
				}
				deriveDisconnect.addEventListener(MouseEvent.CLICK,onRemoveDKRelation);
				
				deriveDisconnectRelation=_DKRelation;
				showDeriveDisconnect=true;
			}
			else
			{
				this.removeChild(deriveDisconnect);
				deriveDisconnect=null;
				deriveDisconnectRelation=null;
				showDeriveDisconnect=false;
			}
			this.invalidateDisplayList();
		}
		
		private function onRemoveDKRelation(evt:MouseEvent):void
		{
			canvas.curProject.removeDK(deriveDisconnectRelation,0);
			
		}
		
		override protected function turnLife():void
		{
			
			var DKList:ArrayList= (DataSourceSkeleton)(this.getSkeleton()).getDKList();
			if(DKList == null)
			{
				return;
			}
			var i:Number=0;
			for(i=0; i<DKList.length; i++)
			{
				var curDK:DKRelationSkeleton = (DKRelationSkeleton)(DKList.getItemAt(i));
				var curKnow:KnowledgeSprite = (KnowledgeSprite)(curDK.getToPointSkeleton().getSprite());
				
				if(curKnow==null)
				{
					return;
				}
				
				if(curDK.getType() == 0)
				{
					curKnow.increaseLife();
				}
				else
				{
					curKnow.increasePoison();
				}
			}
		}
		
		override protected function convertExpand():void
		{
			super.convertExpand();
				if(expand==true)
				{
					deriveDisconnect.y= this.getHeight()/2 - deriveDisconnect.height/2;
				}
				else
				{
					deriveDisconnect.y= MARGIN_HEIGHT;
				}
		}
		
		override public function colorAfter():void
		{
			if(this.getLifeStatus() != 1)
				return;
			
			var DKList:ArrayList = (DataSourceSkeleton)(getSkeleton()).getDKList();
			if(DKList==null)
				return;
			
			var i:int=0;
			for(i=0; i<DKList.length; i++)
			{
				var curDK:DKRelationSkeleton = (DKRelationSkeleton)(DKList.getItemAt(i));
				if(curDK.getType() == 0)
				{
					curDK.getToPointSkeleton().getSprite().increaseLife();
				}
				else
				{
					curDK.getToPointSkeleton().getSprite().increasePoison();	
				}
			}
		}
		
		override public function highlightAfter(_showDelButton:Boolean):void
		{
			var DKList:ArrayList = (DataSourceSkeleton)(getSkeleton()).getDKList();
			if(DKList==null)
				return;
			
			var i:int=0;
			for(i=0; i<DKList.length; i++)
			{
				var curDK:DKRelationSkeleton = (DKRelationSkeleton)(DKList.getItemAt(i));
				curDK.getSprite().setSelected(true);
				curDK.getToPointSkeleton().getSprite().setHighlight(true);
				curDK.getToPointSkeleton().getSprite().highlightAfter(false);
				if(_showDelButton==true)
				{
					curDK.getToPointSkeleton().getSprite().showFromDelButton(curDK);
				}
			}
		}
		
		override public function resetColor():void
		{
			super.resetColor();
			increaseLife();
		}
		
		override public function showDeriveDelButton(_relation:LineSkeleton):void
		{
			deriveDisconnect.visible=true;
			deriveDisconnectRelation = (DKRelationSkeleton)(_relation);
	
		}
		
		override public function resetHighlight():void
		{
			super.resetHighlight();
			deriveDisconnect.visible=false;
		}

		override protected function onChanged(evt:PropertyChangeEvent):void
		{
			canvas.curProject.modifyName(this.getSkeleton(),(String)(evt.newValue),1);
		}
		
		override protected function onDiscriptionChanged(evt:PropertyChangeEvent):void
		{
			canvas.curProject.modifyDiscription(this.getSkeleton(),(String)(evt.newValue),1);
		}
	}
}